package com.audaque.springboot.foshanupload.web.webdemo.controller;

import com.audaque.springboot.foshanupload.core.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author zgb
 * @desc ...
 * @date 2023-08-24 18:22:58
 */
@Slf4j
@RestController
@RequestMapping("file")
public class FlieController {

    @GetMapping("del")
    public void del(@RequestParam String directoryPath,@RequestParam String delDirectoryName) throws IOException {
        File directory = new File(directoryPath);
        FileUtil.del(directory,delDirectoryName);
    }

    @GetMapping("yaml2Prop")
    public void yaml2Prop() throws IOException {
           FileUtil.yaml2Prop("E:\\code\\nestease\\muse\\dev\\muse-service\\business\\src\\main\\resources\\application-dev.yml");
    }



    @GetMapping(value = "/download")
    public void get(HttpServletResponse response) throws IOException {
        String fileName = "yaml-payload.jar";
        ClassPathResource classPathResource = new ClassPathResource("lib/" + fileName);
        InputStream inputStream = classPathResource.getInputStream();


        response.setContentType(response.getContentType());
        response.setHeader("Content-disposition",
                "attachment; filename=" + fileName);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;

        byte[] buffer = new byte[3];
        while ((len = inputStream.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        byte[] bytes = baos.toByteArray();
        response.setHeader("Content-Length", String.valueOf(bytes.length));
        BufferedOutputStream bos = null;
        bos = new BufferedOutputStream(response.getOutputStream());
        bos.write(bytes);
        bos.close();
        baos.close();


    }

}
